*****************************************************************************************
*****************************************************************************************
***Ch 3 - Basic predictors of cost


**************************************************
****Loading data
cd "C:\Users\zmohr\Dropbox\Zach\Election Budgets Research Group\Book\Submission folder\Replication files\" //Set path

use "General Dataset.dta" 



***dropping years less than 2005 because of pre-recessionary period to analyze
drop if Year < 2005

****size
gen newregvoters_thou=newregvoters/1000
//gen newregvoters_sq = newcostperreg_thou*newcostperreg_thou

*linear model
reg realcostperreg newregvoters i.Year i.StateFIPS

*non-linear - Mod1
//Note that the largest county with registered voters is LA county in 2016
reg realcostperreg c.newregvoters_thou##c.newregvoters_thou i.Year i.StateFIPS
//margins, at(newregvoters_thou=(0(100)6800)) atmeans vsquish post
//marginsplot


reg realcostperreg c.newregvoters_thou##c.newregvoters_thou i.Year i.StateFIPS if State=="AK" | State=="CA" | State=="IA" | State=="MD" | State=="ND" | State=="NE" | State=="NY" | State=="SD" | State=="WA"
//margins, at(newregvoters_thou=(0(100)6800)) atmeans vsquish post
//marginsplot

*with cluster see
*non-linear
//Note that the largest county with registered voters is LA county in 2016
reg realcostperreg c.newregvoters_thou##c.newregvoters_thou i.Year i.StateFIPS, vce(cluster CountyFIPS)
//margins, at(newregvoters_thou=(0(100)6800)) atmeans vsquish post
//marginsplot

*multilevel model
mixed  realcostperreg c.newregvoters_thou##c.newregvoters_thou i.Year||StateFIPS:
margins, at(newregvoters_thou=(0(100)6800)) atmeans vsquish post
marginsplot

mixed  realcostperreg c.newregvoters_thou##c.newregvoters_thou i.Year||StateFIPS:
estat icc



//reg realcostperreg c.newregvoters_thou##c.newregvoters_thou i.Year i.StateFIPS if State=="AK" | State=="CA" | State=="IA" | State=="MD" | State=="ND" | State=="NE" | State=="NY" | State=="SD" | State=="WA", vce(cluster CountyFIPS)
//margins, at(newregvoters_thou=(0(100)6800)) atmeans vsquish post
//marginsplot


****Economic Period
recode Year (2005/2007=1 "Pre-recessionary") (2008/2009=2 "Recessionary") (2010/2012=3 "Housing Lag") (2013/2017=4 "Post-recession")(.=1), gen (economic_cond)
 tab economic_cond
reg realcostperreg i.economic_cond, vce(cluster CountyFIPS)
//outreg2 using myreg.doc, replace ctitle(Model 1)

reg realcostperreg i.economic_cond if Year >2007, vce(cluster CountyFIPS)
//outreg2 using myreg.doc, append ctitle(Model 2)


*just in the south alone - fully pooled
reg realcostperreg i.economic_cond if State=="AR" | State=="LA" | State=="MS" | State=="AL"| State=="TN" | State=="VA" | State=="NC" | State=="SC" | State=="GA" | State=="FL", vce(cluster CountyFIPS)

*south with fixed effects
reg realcostperreg i.economic_cond i.StateFIPS if State=="AR" | State=="LA" | State=="MS" | State=="AL"| State=="TN" | State=="VA" | State=="NC" | State=="SC" | State=="GA" | State=="FL", vce(cluster CountyFIPS)

***Partisanship in election years using NANDA data  - DISCUSSED - Do not put in book because of data issues
*gen per_rep = PRES_REP_VOTES/BALLOTS_CAST
*summarize per_rep
*reg realcostperreg per_rep i.StateFIPS i.Year
*reg realcostperreg PRES_REP_RATIO i.StateFIPS i.Year
*reg realcostperreg PARTISAN_INDEX_REP i.StateFIPS i.Year

*partisanship controlling for size w/ year and state FE
*reg realcostperreg per_rep c.newregvoters_thou##c.newregvoters_thou i.StateFIPS i.Year, vce(cluster CountyFIPS)
*reg realcostperreg PRES_REP_RATIO c.newregvoters_thou##c.newregvoters_thou i.StateFIPS i.Year, vce(cluster CountyFIPS)
*reg realcostperreg PARTISAN_INDEX_REP c.newregvoters_thou##c.newregvoters_thou i.StateFIPS i.Year, vce(cluster CountyFIPS)

******REESE VARIABLES? - No, not going there with this analysis

***Robustness tests
*natural log model - identical to mod 1 from above
//gen log_realcostperreg= ln(realcostperreg)
//summarize log_realcostperreg
//reg log_realcostperreg c.newregvoters_thou##c.newregvoters_thou i.Year i.StateFIPS
//margins, at(newregvoters_thou=(0(100)6800)) atmeans vsquish post
//marginsplot
*Findings only get stronger

summarize newregvoters


//recode Year (2008 2016=3 "Presidential") (2014 2012 2010= 2 "National-Non.Pres.") (2009 2011 2013 2015 = 1 "Local election") (.=.), gen(election_type)
//reg realcostperreg i.election_type if Year >2007 & Year <2017
///drop election_type
recode Year (2008 2016 2014 2012 2010=1 "national") (2009 2011 2013 2015 = 0 "Local election") (.=.), gen(election_type)
reg realcostperreg election_type if Year >2007 & Year <2017
*Multiply ND by 1.833 to get sumrealcostperreg value